
#include "mic.h"
#include "rcc.h"

/**
  * @brief  Deinitializes the MICx peripheral registers to their default reset values.
  * @param  MICx: where x can be 0 to select the MICx peripheral.
  * @retval None
  */
void MIC_DeInit(MIC_TypeDef* MICx)
{
	if(MICx == MIC)
	{
		RCC_APB1PeriphResetCmd(RCC_APB1Periph_MIC,ENABLE);
		RCC_APB1PeriphResetCmd(RCC_APB1Periph_MIC,DISABLE);
	}
}

/**
  * @brief  Initializes the MIC Base Unit peripheral according to 
  *         the specified parameters in the MIC_InitStructure.
  * @param  MICx: where x can be 0 to 1 to select the MIC peripheral.
  * @param  MIC_InitStructure: pointer to a MIC_InitTypeDef
  *         structure that contains the configuration information for the 
  *         specified MIC peripheral.
  * @retval None
  */
void MIC_Init(MIC_TypeDef* MICx,MIC_InitTypeDef* MIC_InitStructure)
{
	uint16_t	miccyc_reg = 0;
	uint16_t	micthr_reg = 0;
	uint16_t	micchx_reg = 0;
	
	miccyc_reg = MICx->MICCYC;
	miccyc_reg &= ~MIC_Period_Mask;
	miccyc_reg |= MIC_InitStructure->MIC_Period&MIC_Period_Mask;
	
	micthr_reg = MICx->MICTHR;
	micthr_reg &= ~MIC_THR_Mask;
	micthr_reg |= MIC_InitStructure->MIC_THR&MIC_THR_Mask;
	
	micchx_reg = MICx->MICSEL;
	micchx_reg &= ~MIC_Channel_Mask;
	micchx_reg |= MIC_InitStructure->MIC_Channel&MIC_Channel_Mask;
	micchx_reg |= MIC_InitStructure->MIC_CompareDir&MIC_CompareDir_Mask;
	
	MICx->MICCYC = miccyc_reg;
	MICx->MICTHR = micthr_reg;
	MICx->MICSEL = micchx_reg;
}

/**
  * @brief  Enables or disables the specified MIC peripheral.
  * @param  NewState: new state of the MIC peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void MIC_Cmd(MIC_TypeDef* MICx,FunctionalState NewState)
{	
	if (NewState != DISABLE)
	{
		TK->TKCON |= 0x10;
	}
	else
	{
		TK->TKCON &= ~0x10;
	}	
}

/**
  * @brief  Checks whether the specified MIC flag is set or not.
  * @param  MICx: where x can be 0 to 1 to select the MIC peripheral.
  * @param  MIC_FLAG: specifies the flag to check.
  *   This parameter can be one of the following values:
	*     @arg MIC_FLAG_Compare:    MIC  Complete Flag
  * @note
    
  * @retval The new state of MIC_FLAG (SET or RESET).
  */
FlagStatus MIC_GetFlagStatus(MIC_TypeDef* MICx, uint16_t MIC_FLAG)
{
  ITStatus bitstatus = RESET;  
  
  if ((TK->TKIF & MIC_FLAG) != (uint16_t)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return bitstatus;	
}

/**
  * @brief  Checks whether the specified MIC flag is set or not.
  * @param  MICx: where x can be 0 to 1 to select the MIC peripheral.
  * @param  MIC_FLAG: specifies the flag to check.
  *   This parameter can be one of the following values:
	*     @arg MIC_FLAG_Compare:    MIC  Complete Flag
  * @note  
  * @retval None
  */
void MIC_ClearFlag(MIC_TypeDef* MICx, uint16_t MIC_FLAG)
{  
  /* Clear the flags */
  TK->TKIF = (uint16_t)MIC_FLAG;	
}

/**
  * @brief  Checks whether the specified MIC flag is set or not.
  * @param  MICx: where x can be 0 to 1 to select the MIC peripheral.
  * @param  MIC_FLAG: specifies the flag to check.
  *   This parameter can be one of the following values:
	*     @arg MIC_IT_Compare:   MIC Interrupt source
  * @note
    
  * @retval The new state of MIC_FLAG (SET or RESET).
  */
ITStatus MIC_GetITStatus(MIC_TypeDef* MICx, uint16_t MIC_IT)
{
  ITStatus bitstatus = RESET;  
  uint16_t itstatus = 0x0;
   
  itstatus = TK->TKIF & MIC_IT;
  
  if ((itstatus != (uint16_t)RESET))
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return bitstatus;	
}

/**
  * @brief  Checks whether the specified MIC flag is set or not.
  * @param  MICx: where x can be 0 to 1 to select the MIC peripheral.
  * @param  MIC_FLAG: specifies the flag to check.
  *   This parameter can be one of the following values:
	*     @arg MIC_IT_Compare:   MIC Interrupt source
  * @note    
  * @retval None
  */
void MIC_ClearITPendingBit(MIC_TypeDef* MICx, uint16_t MIC_IT)
{
  /* Clear the IT pending Bit */
  TK->TKIF = (uint16_t)MIC_IT;	

}


/**
  * @brief  Checks whether the specified MIC flag is set or not.
  * @param  MICx: where x can be 0 to select the MIC peripheral.
  * @note
    
  * @retval The MIC_CH data.
  */
uint16_t MIC_GetData(MIC_TypeDef* MICx)
{
	return (uint16_t)MICx->MICDATA;
}

/**
  * @brief  Config the MICx MIC_Channelx Remap MIC_CHSELINNx  Source.
  * @param  MICx: where x can be 0  to select the MIC peripheral.
  * @param  MIC_Channelx: specifies the flag to check.
  *   This parameter can be one of the following values:
  *     @arg MIC_Channel_NULL,MIC_Channel_0 ~~ MIC_Channel_x.
  * @note    
  * @retval None
  */
void MIC_ChannelConfig(MIC_TypeDef* MICx,uint16_t MIC_Channelx)
{
	uint16_t micsel_reg = 0;
	
	micsel_reg = MICx->MICSEL;
	micsel_reg &= ~MIC_Channel_Mask;
	micsel_reg |=  MIC_Channelx&MIC_Channel_Mask;
	
	MICx->MICSEL = micsel_reg;
}
/**
  * @brief  Set mic thr cmp dir specified MIC.
  * @param  MICx: where x can be 0  to select the MIC peripheral.
  * @param  MIC_Dir: specifies the flag to check.
  *   This parameter can be one of the following values:
  *     @arg MIC_CompareDir_Up,MIC_CompareDir_Down.
  * @note    
  * @retval None
  */
void MIC_SetCompareDir(MIC_TypeDef* MICx,uint16_t MIC_CompareDir) 
{
	uint16_t micsel_reg = 0;
	
	micsel_reg = MICx->MICSEL;
	micsel_reg &= ~MIC_CompareDir_Mask;
	micsel_reg |=  MIC_CompareDir&MIC_CompareDir_Mask;
	
	MICx->MICSEL = micsel_reg;
}
